package forms;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.upload.FormFile;

public class FileUploadForm extends ActionForm {

	private static final long serialVersionUID = 1L;
	
	private String stuState = null;
	private String school = null;
	private String discp = null;
	private String dmonth = null;
	private String dyear = null;
	private String gpa = "";
	private String title = null;
	private String fileName = null;
	private int fileSize = 0;
	private InputStream myFile = null;
	private FormFile file = null;
	private boolean moredegree = false;
	private String count = "first";
	private List<degreecollection> degrees = new ArrayList<degreecollection>();
	private int appID = 0;
	private String content = "";
	
	private String specialization = null;
	
	public void resetdiscp(){
		this.stuState = null;
		this.school = null;
		this.discp = null;
		this.dmonth = null;
		this.dyear = null;
		this.gpa = "";
		this.title = null;
		this.myFile = null;
		this.fileSize = 0;
	}

	public int getFileSize() {
		return fileSize;
	}

	public void setFileSize(int fileSize) {
		this.fileSize = fileSize;
	}

	public void updateCollection(degreecollection newdegree){
		this.degrees.add(newdegree);
	}
	

	public List<degreecollection> getDegrees() {
		return degrees;
	}


	public int getAppID() {
		return appID;
	}

	public void setAppID(int appID) {
		this.appID = appID;
	}

	public void setDegrees(List<degreecollection> degrees) {
		this.degrees = degrees;
	}

	public String getCount() {
		return count;
	}

	public void setCount(String count) {
		this.count = count;
	}

	public String getSpecialization() {
		return specialization;
	}
	
	public void setSpecialization(String specialization) {
		this.specialization = specialization;
	}

	public boolean isMoredegree() {
		return moredegree;
	}

	public void setMoredegree(boolean moredegree) {
		this.moredegree = moredegree;
	}

	public String getDmonth() {
		return dmonth;
	}

	public void setDmonth(String dmonth) {
		this.dmonth = dmonth;
	}

	public String getDyear() {
		return dyear;
	}

	public void setDyear(String dyear) {
		this.dyear = dyear;
	}

	public String getGpa() {
		return gpa;
	}

	public void setGpa(String gpa) {
		this.gpa = gpa;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public InputStream getMyFile() {
		return myFile;
	}

	public void setMyFile(InputStream myFile) {
		this.myFile = myFile;
	}
	
	public FormFile getFile() {
		return file;
	}
	
	public void setFile(FormFile file) {
		this.file = file;
	}

	public String getDiscp() {
		return discp;
	}

	public void setDiscp(String discp) {
		this.discp = discp;
	}

	public String getSchool() {
		return school;
	}

	public void setSchool(String school) {
		this.school = school;
	}

	public String getStuState() {
		return stuState;
	}

	public void setStuState(String stuState) {
		this.stuState = stuState;
	}
	
	// check if first name, middle initial, and last name are provided and valid
	public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();

		char[] gpaArr = gpa.toCharArray();
		boolean invalid = false;
		for(int i = 0; i < gpaArr.length; i++ )
		{
			if( (gpaArr[i] >= 48) && (gpaArr[i] <= 57) )
			{
				invalid = false;
			}
			else
			{
				invalid = true;
			}
		}
		
		if( invalid )
		{
			errors.add("first", new ActionMessage("error.gpa.invalid"));
			setGpa("");
		}
		
		if( file != null )
		{
			content = file.getContentType();
		}
		
		if( content != "" )
		{
			if( !content.equals("application/pdf") )
			{
				errors.add("content", new ActionMessage("error.transcript.invalid"));
				content = "";
			}
		}

		return errors;
	}
	
	
	
}
